package cn.tedu.demospider.service.impl;

import cn.tedu.demospider.service.SpiderService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpiderServiceImpl implements SpiderService {

    private static final Logger logger = LoggerFactory.getLogger(SpiderServiceImpl.class);

    @Autowired
    WebDriver webDriver;

    @Override
    public void loadJobs()  {
        //爬取网站内容
        String index = "https://www.zhipin.com/job_detail/?query=java&city=100010000";
        webDriver.get(index);

        String html = webDriver.getPageSource();
        //logger.debug("{}", html);
        //将下载的网页交给 Jsoup 进行解析
        Document doc = Jsoup.parse(html);
        //查找那些招聘信息
        List<Element> elements = doc.select(".job-name a");
        for (Element e:elements) {
            //logger.debug("工作详情页面：{}", e.attr("href"));
            String url = "https://www.zhipin.com"+e.attr("href");
            logger.debug("URL: {}", url);
            loadJobDetail(url);
        }
    }
    private void loadJobDetail(String url){
        webDriver.get(url);
        String html = webDriver.getPageSource();
        Document doc = Jsoup.parse(html);
        List<Element> elements = doc.select(".detail-content .job-sec");
        for (Element e: elements){
            logger.debug("Job: {}", e.text());
        }
    }
}
